//
//  OneAuthorizeCodeView.m
//  lTTheOne
//
//  Created by sy on 2025/7/21.
//

#import "OneAuthorizeCodeView.h"
#import "OneMultilinguaItem.h"
#import "HeaderConfig.h"
#import <Masonry/Masonry.h>

@interface OneAuthorizeCodeView ()
@property(nonatomic, strong) OneSetMenuItem *headMenu;
@property(nonatomic, strong) UIStackView *stackView;

@property(nonatomic, strong) OneSetMenuItem *authorizeDesView;
@property(nonatomic, strong) OneSetMenuItem *codeSetView;

@property(nonatomic, strong) OneCodeSetMenuItem *inputView;
@property(nonatomic, strong) OneMultilinguaItem *sureItem;

@property(nonatomic, strong) OneAuthorizeCodeModel *codeModel;
@property(nonatomic, strong) OneAuthorizeConfigModel *configModel;

@property (nonatomic,assign) authorizeState codeState;
@property (nonatomic,assign) authorizeCode codeType;

@end

@implementation OneAuthorizeCodeView
- (instancetype)initWithFrame:(CGRect)frame withCodeType:(authorizeCode)codeType withState:(authorizeState)codeState
{
    self = [super init];
    if (self) {
        self.codeType = codeType;
        self.codeState = codeState;
        
        self.configModel = [[OneAuthorizeConfigModel alloc]initWithAuthorizeCode:codeType];
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{

    self.headMenu = [self createMenuItem];
    self.headMenu.chinaLB.text = self.configModel.title_ch;
    self.headMenu.englishLB.text = self.configModel.title_en;
    [self.headMenu showTitleFont:KFont_M(18) withEnFont:KFont_M(8)];
    [self addSubview:self.headMenu];
    
    UIStackView *stackView = UIStackView.new;
    stackView.axis = UILayoutConstraintAxisVertical;
    stackView.distribution = UIStackViewDistributionEqualSpacing;
    stackView.spacing = 18;
    [self addSubview:stackView];
    self.stackView = stackView;
    
    if (self.codeState == setCode) {
        self.authorizeDesView = [self createMenuItem];
        self.authorizeDesView.chinaLB.text = self.configModel.desc_ch;
        self.authorizeDesView.englishLB.text = self.configModel.desc_en;
        
        [self.stackView addArrangedSubview:self.authorizeDesView];
    }
   
    self.codeSetView = [self createMenuItem];
    if (self.codeState == setCode) {
        self.codeSetView.chinaLB.text = self.configModel.setCode_ch;
        self.codeSetView.englishLB.text = self.configModel.setCode_en;
    }else if (self.codeState == checkCode){
        self.codeSetView.chinaLB.text = self.configModel.checkCode_ch;
        self.codeSetView.englishLB.text = self.configModel.checkCode_en;
    }
    [self.stackView addArrangedSubview:self.codeSetView];
    
    [self.stackView setCustomSpacing:0 afterView:self.codeSetView];
    
    self.inputView = [[OneCodeSetMenuItem alloc]initWithFrame:CGRectZero];
    [self.stackView addArrangedSubview:self.inputView];

    
    self.sureItem = [[OneMultilinguaItem alloc]initWithFrame:CGRectZero];
    [self.sureItem showBackGroundColor:KUIColorFromRGB(0xFFC301)];
    [self.sureItem showTitleStr:@"确  定" withEnStr:@"Enter"];
    UIColor *textColor = KUIColorFromRGB(0x000000);
    [self.sureItem showTitleColor:textColor withEnStr:textColor];
    [self addSubview:self.sureItem];
   
    UITapGestureRecognizer *tap_sureItem = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sureItemClickAction)];
    [self.sureItem addGestureRecognizer:tap_sureItem];
}

- (OneSetMenuItem*)createMenuItem{
    OneSetMenuItem *menuItem = [[OneSetMenuItem alloc]initWithFrame:CGRectZero];
    [menuItem showNomalStyle];
    menuItem.chinaLB.textColor = KUIColorFromRGB(0xFFFFFF);
    menuItem.englishLB.textColor = KUIColorFromRGB(0xFFFFFF);
    [menuItem showTitleFont:KFont_M(18) withEnFont:KFont_M(10)];
    [menuItem showTextAlignment:NSTextAlignmentLeft];
    
    return menuItem;
}

- (void)layoutSubviews{
    [self.headMenu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(16);
    }];
    
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(18);
        make.centerX.equalTo(self);
        make.top.equalTo(self.headMenu.mas_bottom).offset(10);
    }];
    
    [self.sureItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-18);
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(64);
    }];
}
- (void)sureItemClickAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(sureActionCodeType:withState:withCodeText:)]) {
        [self.delegate sureActionCodeType:self.codeType withState:self.codeState withCodeText:self.inputView.codeTextField.text];
    }
}
@end
